package hang.com.mvptest.login.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.android.tpush.service.XGPushServiceV3;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import hang.com.mvptest.MainActivity;
import hang.com.mvptest.R;
import hang.com.mvptest.login.presenter.ILoginPresenter;
import hang.com.mvptest.login.presenter.LoginPresenterCompl;
import icepick.Icepick;
import icepick.State;

/**
 * Created by hang on 2017/2/22.
 */

public class LoginActivity extends AppCompatActivity implements ILoginView {

    private static final String APP_ID = "wx376b094cbb736f84";
    private IWXAPI api;

    @BindView(R.id.login_et_username)
    EditText editUser;
    @BindView(R.id.login_et_password)
    EditText editPass;
    @BindView(R.id.login_btn_login)
    Button btnLogin;
    @BindView(R.id.login_btn_clean)
    Button btnClean;
    @BindView(R.id.login_progressBar_login)
    ProgressBar progressBar;

    ILoginPresenter loginPresenter;

    @State
    String username;
    @BindView(R.id.textView5)
    TextView tvResult;
    @BindView(R.id.login_btn_wx)
    Button loginBtnWx;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //
        Icepick.restoreInstanceState(this, savedInstanceState);
        ButterKnife.bind(this);

        //init
        loginPresenter = new LoginPresenterCompl(this);
        progressBar.setVisibility(View.INVISIBLE);


        //推送
        XGPushConfig.enableDebug(this,true);
        Context context = getApplicationContext();
        XGPushManager.registerPush(context, new XGIOperateCallback() {
            @Override
            public void onSuccess(Object o, int i) {
                String mToken = (String)o;
                Log.d("TPush", "获取token：" + mToken);
            }

            @Override
            public void onFail(Object o, int i, String s) {
                Log.d("TPush", "获取token失败：");
            }
        });


    }

    @OnClick(R.id.login_btn_login)
    void submit() {
        loginPresenter.setProgressBarVisibility(View.VISIBLE);
        btnLogin.setEnabled(false);
        btnClean.setEnabled(false);
        loginPresenter.doLogin(editUser.getText().toString(), editPass.getText().toString());
    }

    @OnClick(R.id.login_btn_clean)
    void clean() {
        loginPresenter.clean();

    }

    @OnClick(R.id.login_btn_wx)
    void wxLogin(){
        Toast.makeText(this,"微信登录",Toast.LENGTH_SHORT).show();
        //注册微信
        api = WXAPIFactory.createWXAPI(this,APP_ID,true);
        //
        Boolean suc = api.registerApp(APP_ID);

        Toast.makeText(this, String.format("注册微信%s",suc?"成功":"失败"),Toast.LENGTH_LONG).show();

        //微信登陆
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "mvptest";
        suc = api.sendReq(req);
        Toast.makeText(this, String.format("登陆微信%s",suc?"成功":"失败"),Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Icepick.saveInstanceState(this, outState);
    }

    @Override
    public void onCleanText() {
        editUser.setText("");
        editPass.setText("");
    }

    @Override
    public void onLoginResult(Boolean result, int code) {
        loginPresenter.setProgressBarVisibility(View.INVISIBLE);
        btnLogin.setEnabled(true);
        btnClean.setEnabled(true);
        if (result) {
            Toast.makeText(this, "Login success", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, MainActivity.class));
        } else {
            Toast.makeText(this, "Login fail,code:" + code, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onSetProgressBarVisibility(int visibility) {
        progressBar.setVisibility(visibility);
    }
}
